/*******************************************************************************
 This MASTER do file is to replicate outcomes for the research paper 
 
	"Shortening the Path to Productive Investment:
		Evidence from Input Fairs and Cash Transfers in Malawi"
	
 It does the following:
  0. set up working directory and install required Stata packages
  1. generate tables in the paper
 
 Related folder paths:
  - where figures exported: 	/output/
 
 2024.08.17 (David S. Park) Created
*******************************************************************************/
clear all
set more off
set maxvar 32000

/*******************************************************************************
 0. Set up working directory and install required Stata packages
*******************************************************************************/
// root folder (REPLICATOR: change this to where the replication package folder is saved)
cap noisily cd "/Users/DSP/Dropbox/Research/Projects/GD cashplus analysis/Replication/HarvardDataverse_replication"

// folder to export tables
global raw_folder "output"

// install Stata packages required to run replication
ssc install estout
*******************************************************************************/

	// Control variables in regressions
	global controls total_assets_usd_r total_assets_usd_m years_educ_r years_educ_m

/*******************************************************************************	
 Table 1: Summary Statistics and Randomization check
*******************************************************************************/
local texname table1

use "replicationData.dta", clear
		
// Panel A. Demographics
local panel_text "Panel A. Demographics"
local yvarlist_a age female years_educ hh_size 

est clear
local i = 0
foreach var of local yvarlist_a {
	macro drop vars
	global vars `var'_r
	local ++i
	cap drop x1
	gen x1 = `var'_r
	eststo model_a`i': estpost sum x1 if GD_only_raw==0 
		
	replace x1 = GD_only_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_x`i':  reghdfe `var'_r x1, cluster(village)  absorb(strata)

	replace x1 = MA_only_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_y`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)
	
	replace x1 = GD_MA_both_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_z`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)	
	
	#d ;
	if "$vars" == "age_r" { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i' using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) 
		label numbers nobaselevels keep(x1) 
		prehead("\begin{tabular}{l c ccc}" "\toprule" )
		posthead(	
			" & \multicolumn{1}{c}{\begin{tabular}{c} Mean \\ (std. dev.) \end{tabular}} & \multicolumn{3}{c}{\begin{tabular}{c} Coefficient on Difference \\ (Treatment - Control) \end{tabular}}  \\"
			"\cmidrule(lr){2-2}\cmidrule(lr){3-5}"
			" & \multicolumn{1}{c}{\begin{tabular}{c} Control \end{tabular}} & \multicolumn{1}{c}{\begin{tabular}{c} Cash only \end{tabular}} & \multicolumn{1}{c}{\begin{tabular}{c} Input fairs \\ only \end{tabular}} & \multicolumn{1}{c}{\begin{tabular}{c} Cash + Input \\ fairs \end{tabular}}  \\"
			"\midrule"
			)
		cells("mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(1 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 	;			
	} ;
	
	// not reporting SD (for binary outcomes)
	else if "$vars" == "female_r" { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i' using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers keep(x1)
		cells( "mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(0 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 	;			
	};			

	else { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i' using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers keep(x1)
		cells( "mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(1 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 	;			
	};			

	#d cr	
} 	

// Panel B. Expenditure and Wealth
local panel_text "Panel B. Expenditure and Wealth"
local yvarlist_b total_expend_usd_mon total_assets_usd 
 
est clear
local i = 0
foreach var of local yvarlist_b {
	macro drop vars
	global vars `var'_r
	local ++i
	cap drop x1
	gen x1 = `var'_r
	eststo model_a`i': estpost sum x1 if GD_only_raw==0 
				
	replace x1 = GD_only_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_x`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)

	replace x1 = MA_only_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_y`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)
	
	replace x1 = GD_MA_both_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_z`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)
	
	#d ;
	if "$vars" == "total_expend_usd_mon_r" { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i'  using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers keep(x1)
		cells( "mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(1 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 			
		posthead();
	};
		
	if "$vars" != "total_expend_usd_mon_r" { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i' using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers keep(x1)
		cells( "mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(1 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 	;			
	};		
	#d cr	
}
		
// Panel C. Agriculture Activity
local panel_text "Panel C. Agriculture Activity"
local yvarlist_c own_farm_land farm_land_size_w1 fert_use_yn_2018 fert_expend_usd_2018  
 
est clear
local i = 0
foreach var of local yvarlist_c {
	macro drop vars
	global vars `var'_r
	local ++i
	cap drop x1
	gen x1 = `var'_r
	eststo model_a`i': estpost sum x1 if GD_only_raw==0 
		su x1 if GD_only_raw==0 
		estadd scalar obs`i' = r(N) 
	
	replace x1 = GD_only_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_x`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)
		su `var'_r if e(sample) & GD_only_raw==1
		estadd scalar obs`i' = r(N)
		
	
	replace x1 = MA_only_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_y`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)
		su `var'_r if e(sample) & MA_only_raw==1
		estadd scalar obs`i' = r(N)
	
	replace x1 = GD_MA_both_raw
	local name: variable label `var'_r
	label var x1 "`name'"
	eststo model_z`i': quietly reghdfe `var'_r x1, cluster(village)  absorb(strata)
		su `var'_r if e(sample) & GD_MA_both_raw==1 
		estadd scalar obs`i' = r(N)

	#d ;
	// not reporting SD (for binary outcomes)
	if "$vars" == "own_farm_land_r" | "$vars" == "fert_use_yn_2018_r" { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i' using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers keep(x1)
		cells( "mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(0 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 				
		posthead();
	};
				
	else { ;
		estout model_a`i' model_x`i' model_y`i' model_z`i'  using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers keep(x1)
		cells( "mean(fmt(%9.2f) pattern(1 0 0 0)) b(star fmt(%9.2f) pattern (0 1 1 1))" "sd(par fmt(%9.2f) pattern(1 0 0 0)) se(par fmt(%9.2f) pattern ( 0 1 1 1))") starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */ 	;			
	};
			
	// Observations
	if  "$vars"=="fert_expend_usd_2018_r" {;
		estout model_a`i' model_x`i' model_y`i' model_z`i' using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none)
		label  nonumbers drop(*)
				postfoot("\bottomrule" "\end{tabular}")
		;		
	};		
			
	#d cr	
	}		
*******************************************************************************/
	

/*******************************************************************************
 Table 2: Fertilizer Purchases at Input Fair
*******************************************************************************/
local texname table2
local yvarlist fert_use_yn fert_expend_usd fert_bought_kg
local numcol = wordcount("`yvarlist'")

use "replicationData.dta", clear

// Panel A. Pooled regression
local panel_text "Panel A. Pooled regression"

est clear
local i = 0
foreach yvar in `yvarlist' {
	local ++i
	// Study sample: voucher FEs 
	eststo : reg ma_`yvar'_r GD_treat voucher_???_perKM i.strata ${controls} if ma_sample==1, cluster(village)
	su ma_`yvar'_r if e(sample) & GD_treat==0 
	estadd scalar gd_controlmean = r(mean)
	//estadd local voucher_fe "Yes"
}

#d;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute( \$  \\$  %  \%  Z $ xx $\times$) 
	keep(GD_treat)
	stats(gd_controlmean N, labels("Control mean" "Observations") fmt(%9.2f %9.0fc))
	starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l *{`numcol'}{c}}" "\toprule")
	posthead( 
		" & \multicolumn{1}{c}{\begin{tabular}{c} =1 if purchased \\ fertilizer \end{tabular}} & \multicolumn{1}{c}{\begin{tabular}{c} Expenditure \\ (USD) \end{tabular}} & \multicolumn{1}{c}{\begin{tabular}{c} Amount \\ (kg) \end{tabular}} \\"
		"\midrule" 
		"\multicolumn{`numcol'}{l}{\textbf{`panel_text'}} \\"
		)
	prefoot("\\")
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	;
#d cr

// Panel B. Interaction with FISP beneficiary status
local panel_text "Panel B. Interaction with FISP beneficiary status"

gen GD_FISP = GD_treat*fisp_hh_2019

lab var GD_treat 		"Cash (Z\betaZ)"
lab var fisp_hh_2019  	"FISP (Z\gammaZ)"
lab var GD_FISP			"Cash Z\timesZ FISP (Z\deltaZ)"

est clear
local i = 0
foreach yvar in `yvarlist' {
	local ++i
	// Study sample: voucher FEs 
	eststo : reg ma_`yvar'_r GD_treat fisp_hh_2019 GD_FISP voucher_???_perKM i.strata ${controls} if ma_sample==1, cluster(village)
		su ma_`yvar'_r if e(sample) & GD_treat==0 & fisp_hh_2019==0
		estadd scalar gd_controlmean = r(mean)
		test GD_treat + fisp_hh_2019 = 0
		estadd scalar pval1 = r(p)		
	
}

#d;
estout * using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none) label 
	substitute( \$  \\$  %  \%  Z $ xx $\times$) 
	keep(GD_treat fisp_hh_2019 GD_FISP)
	order(GD_treat fisp_hh_2019 GD_FISP)
	stats(pval1 gd_controlmean N, labels("\emph{p}-value: \\ \quad Z\beta + \delta = 0Z" "Control mean" "Observations") fmt(%9.3f %9.2f %9.0fc))
	starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	posthead( 
		"\\" 
		"\multicolumn{`numcol'}{l}{\textbf{`panel_text'}} \\"
		)
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")
	;
#d cr
*******************************************************************************/


/*******************************************************************************
 Table 3: Fertilizer Adoption 
*******************************************************************************/
local texname table3
local yvarlist fert_expend_usd fert_bought_kg fert_expend_mkt_usd fert_bought_mkt_kg fert_net_fisp_usd fert_net_fisp_kg 
local numcol = wordcount("`yvarlist'")

use "replicationData.dta", clear

drop GD_only MA_only GD_MA_both

gen GD_only = (GD_treat==1 & MA_treat==0)
gen MA_only = (GD_treat==0 & MA_treat==1)
gen GD_MA_both = (GD_treat==1 & MA_treat==1)
	
lab var GD_only "Cash only (Z\betaZ)"
lab var MA_only "Input fair only (Z\gammaZ)"
lab var GD_MA_both "Cash + Input fair (Z\deltaZ)"

est clear
local i = 0
foreach yvar in `yvarlist'{
	local ++i
		
	eststo : reg `yvar'_2019 GD_only MA_only GD_MA_both `yvar'_2018  i.strata  ${controls} if reg_sample==1, cluster(village) 
	
		su `yvar'_2019 if e(sample) & GD_treat==0 & MA_treat==0
		estadd scalar pure_control_mean = r(mean) 
		estadd scalar pure_control_sd = r(sd) 
					
		test GD_only = GD_MA_both 
		estadd scalar pval2 = r(p)
		
}
		
#d ;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 
	keep(GD_only MA_only GD_MA_both)
	order(GD_only MA_only GD_MA_both)
	stats(pval2 pure_control_mean pure_control_sd N, labels("\emph{p}-value: \\ \quad Z\beta = \deltaZ" "Control mean" "Control SD" "Observations") fmt(%9.3f %9.2f %9.2f %9.0fc)) starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l *{`numcol'}{c}}" "\toprule")
	posthead(
		" & \multicolumn{2}{c}{Total} & \multicolumn{2}{c}{Market} & \multicolumn{2}{c}{FISP} \\"
		"\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}"
		" & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular} & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular} & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular}\\" 
		"\midrule" 
		)		
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")	
	;
	
#d cr
*******************************************************************************/


/*******************************************************************************
 Table 4: Fertilizer Adoption, by FISP beneficiary status
*******************************************************************************/
local texname table4
local yvarlist fert_expend_usd fert_bought_kg fert_expend_mkt_usd fert_bought_mkt_kg fert_net_fisp_usd fert_net_fisp_kg 
local numcol = wordcount("`yvarlist'")

use "replicationData.dta", clear

// Panel A. Non-FISP beneficiaries
local panel_text "Panel A. Non-FISP beneficiaries"

est clear
foreach yvar in `yvarlist'{
	eststo : reg `yvar'_2019 GD_only MA_only GD_MA_both `yvar'_2018  i.strata  ${controls} if fisp_hh_2019==0 & reg_sample==1, cluster(village) 
	
		su `yvar'_2019 if e(sample) & GD_treat==0 & MA_treat==0
		estadd scalar pure_control_mean = r(mean) 
		estadd scalar pure_control_sd = r(sd) 
					
		test GD_only = GD_MA_both 
		estadd scalar pval2 = r(p)
}
		
#d ;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 
	keep(GD_only MA_only GD_MA_both)
	order(GD_only MA_only GD_MA_both)
	stats(pval2 pure_control_mean pure_control_sd N, labels("\emph{p}-value: \\ \quad Z\beta = \deltaZ" "Control mean" "Control SD" "Observations") fmt(%9.3f %9.2f %9.2f %9.0fc)) starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l *{`numcol'}{c}}" "\toprule")
	posthead(
		" & \multicolumn{2}{c}{Total} & \multicolumn{2}{c}{Market} & \multicolumn{2}{c}{FISP} \\"
		"\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}"
		" & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular} & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular} & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular}\\" 
		"\midrule" 
		"\multicolumn{`numcol'}{l}{\textbf{`panel_text'}} \\"
		)
	prefoot("\\")
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	;
#d cr

// Panel B. FISP beneficiaries
local panel_text "Panel B. FISP beneficiaries"

est clear
foreach yvar in `yvarlist'{
	eststo : reg `yvar'_2019 GD_only MA_only GD_MA_both `yvar'_2018  i.strata  ${controls} if fisp_hh_2019==1 & reg_sample==1, cluster(village) 
	
		su `yvar'_2019 if e(sample) & GD_treat==0 & MA_treat==0
		estadd scalar pure_control_mean = r(mean) 
		estadd scalar pure_control_sd = r(sd) 
					
		test GD_only = GD_MA_both 
		estadd scalar pval2 = r(p)
}
		
#d ;
estout * using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none) label 
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 
	keep(GD_only MA_only GD_MA_both)
	order(GD_only MA_only GD_MA_both)
	stats(pval2 pure_control_mean pure_control_sd N, labels("\emph{p}-value: \\ \quad Z\beta = \deltaZ" "Control mean" "Control SD" "Observations") fmt(%9.3f %9.2f %9.2f %9.0fc)) starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	posthead(
		"\\" 
		"\multicolumn{`numcol'}{l}{\textbf{`panel_text'}} \\"
		)		
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")	
	;
#d cr
*******************************************************************************/


/*******************************************************************************
 Table 5: Heterogeneity by Size of Cash Transfer
*******************************************************************************/
local texname table5
local yvarlist fert_expend_usd fert_bought_kg
local numcol = wordcount("`yvarlist'")

use "replicationData.dta", clear

gen GD_treat_250_only = GD_treat==1 & MA_treat==0 & GD_treat_amount==250
gen GD_treat_500_only = GD_treat==1 & MA_treat==0 & GD_treat_amount==500
gen GD_treat_750_only = GD_treat==1 & MA_treat==0 & GD_treat_amount==750
gen MA_treat_only = GD_treat==0 & MA_treat==1
gen GD_MA_250_both = GD_treat==1 & MA_treat==1 & GD_treat_amount==250
gen GD_MA_500_both = GD_treat==1 & MA_treat==1 & GD_treat_amount==500
gen GD_MA_750_both = GD_treat==1 & MA_treat==1 & GD_treat_amount==750

lab var GD_treat_250_only 	"Cash \$250 only (Z\beta_1Z)"
lab var GD_treat_500_only 	"Cash \$500 only (Z\beta_2Z)"
lab var GD_treat_750_only 	"Cash \$250 only (Z\beta_3Z)"
lab var MA_treat_only 		"Input fair only (Z\gammaZ)"
lab var GD_MA_250_both 		"Cash \$250 + input fair (Z\delta_1Z)"
lab var GD_MA_500_both 		"Cash \$500 + input fair (Z\delta_2Z)" 
lab var GD_MA_750_both 		"Cash \$750 + input fair (Z\delta_3Z)" 

est clear
local i = 0
foreach yvar in `yvarlist'{
	local ++i
		
	// Cash disaggregated
	eststo : reg `yvar'_2019 GD_treat_250_only GD_treat_500_only GD_treat_750_only MA_treat_only GD_MA_250_both GD_MA_500_both GD_MA_750_both `yvar'_2018  ${controls} i.strata if reg_sample==1, cluster(village) 
	
		su `yvar'_2019 if e(sample) & GD_treat_250==0 & GD_treat_500==0 & GD_treat_750==0 & MA_treat==0 
		estadd scalar pure_control_mean = r(mean) 
		estadd scalar pure_control_sd = r(sd) 
		
		test GD_treat_250_only = GD_MA_250_both
		estadd scalar pval1 = r(p) 

		test GD_treat_500_only  = GD_MA_500_both
		estadd scalar pval2 = r(p) 

		test GD_treat_750_only = GD_MA_750_both
		estadd scalar pval3 = r(p) 		
}

#d ;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 
	keep(GD_treat_250_only GD_treat_500_only GD_treat_750_only MA_treat_only GD_MA_250_both GD_MA_500_both GD_MA_750_both)
	order(GD_treat_250_only GD_treat_500_only GD_treat_750_only MA_treat_only GD_MA_250_both GD_MA_500_both GD_MA_750_both)
	stats(pval1 pval2 pval3 pure_control_mean pure_control_sd N, labels("\emph{p}-values: \\ \quad Z \beta_1 = \delta_1 Z" "\quad Z \beta_2 = \delta_2 Z" "\quad Z \beta_3 = \delta_3 Z"  "Control mean" "Control SD" "Observations") fmt(%9.3f %9.3f %9.3f %9.2f %9.2f %9.0fc)) starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l *{`numcol'}{c}}" "\toprule")
	posthead(
		" & \begin{tabular}{c} Expenditure \\ (USD) \end{tabular} & \begin{tabular}{c} Amount \\ (kg) \end{tabular} \\" 
		"\midrule" 
		)
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")	
	;

#d cr
*******************************************************************************/


/************************************************************************************************************************
													Appendix Tables
************************************************************************************************************************/

/*******************************************************************************
 Table A1: Endline Survey Attrition
*******************************************************************************/
local texname tableA1
local yvarlist in_endline
local latex_header ///
	" & \multicolumn{1}{c}{\begin{tabular}{c} =1 if completed \\ endline survey \end{tabular}} \\" 

use "replicationData_attrition.dta", clear

local i = 0
est clear
foreach yvar in `yvarlist' {
		local ++i
		eststo: reg `yvar' GD_only MA_only GD_MA_both i.strata, cluster(village_id)
			su `yvar' if GD_treat==0 & 	MA_treat==0
			estadd scalar controlmean = r(mean)			
			su `yvar' 	
			estadd scalar overallmean = r(mean)	
}

#d;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	keep(GD_only MA_only GD_MA_both)
	order(GD_only MA_only GD_MA_both)
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 	
	starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	stats(controlmean overallmean N, labels("Pure control mean" "Overall mean" "Observations") fmt(%9.2fc %9.2fc %9.0fc))  
	prehead("\begin{tabular}{l *{`i'}{c}}" "\toprule")
	posthead("`latex_header'" "\midrule")	
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")
	;
#d cr
*******************************************************************************/


/*******************************************************************************
 Table A2: Attendance at Input Fair by Travel Voucher Amount
*******************************************************************************/
local texname tableA2
local yvarlist voucher_redeem_r 

use "replicationData.dta", clear

lab var voucher_200_perKM 	"MWK200 per km (Z\beta_1Z)"
lab var voucher_400_perKM 	"MWK400 per km (Z\beta_2Z)"
lab var voucher_600_perKM 	"MWK600 per km (Z\beta_2Z)"
 
est clear
foreach yvar in `yvarlist' {
	eststo : reg `yvar' voucher_200_perKM voucher_400_perKM voucher_600_perKM /*`yvar'_2018_r*/ i.village if ma_sample==1 /* & fisp_hh_2018_r==0 */, cluster(id)
		su `yvar' if e(sample) & voucher_100_flat
		estadd scalar flat_mean = r(mean)

}

#d;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute( \$  \\$  %  \%  Z $ xx $\times$) 
	order(voucher_200_perKM voucher_400_perKM voucher_600_perKM)
	keep(voucher_200_perKM voucher_400_perKM voucher_600_perKM)
	stats(flat_mean N, labels("Flat MWK100: Mean" "Observations") fmt(%9.2f %9.0fc))
	starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l c}" "\toprule")
	posthead( 
	" & \multicolumn{1}{c}{\begin{tabular}{c} =1 if attended \\ input fair \end{tabular}} \\"
	 "\midrule" )
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")
	;
#d cr
*******************************************************************************/


/*******************************************************************************
 Table A3: Take-up by Type of Input
*******************************************************************************/
local texname tableA3

use "replicationData.dta", clear

preserve
keep if MA_treat==1

// Any input
local header "Any input"
local yvarlist ma_any_input_yn_r ma_total_input_expend_usd_r
lab var ma_any_input_yn_r 			"\quad =1 if bought any"
lab var ma_total_input_expend_usd_r "\quad Amount spent (USD)"

est clear
eststo entire: 		estpost su `yvarlist'
eststo treatment:   estpost su `yvarlist' if GD_treat == 1  
eststo control:   	estpost su `yvarlist' if GD_treat == 0


#d ;
estout entire control treatment using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	prehead("\begin{tabular}{l ccc}" "\toprule")
	posthead(
		" & \begin{tabular}{c} Total \\ input fair \\ sample \end{tabular} & \begin{tabular}{c} Input fair \\ treatment \\ only \end{tabular} & \begin{tabular}{c} Cash + \\ input fair \end{tabular} \\ "
		"\midrule"
		"\multicolumn{3}{l}{\textbf{`header'}} \\"
		)
	cells(
		"mean(fmt(%9.2fc) pattern(1 1 1))" 
		"sd(par fmt(%9.2fc) pattern(1 1 1) drop(*_yn_*))")  
	;
#d cr	

// Chemical fertilizer
local header "Chemical fertilizer"
local yvarlist ma_fert_use_yn_r ma_fert_expend_usd_r
lab var ma_fert_use_yn_r 		"\quad =1 if bought any"
lab var ma_fert_expend_usd_r 	"\quad Amount spent (USD)"

est clear
eststo entire: 		estpost su `yvarlist'
eststo treatment:   estpost su `yvarlist' if GD_treat == 1  
eststo control:   	estpost su `yvarlist' if GD_treat == 0

#d ;
estout entire control treatment using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none) label 
	posthead(
		"\\"
		"\multicolumn{3}{l}{\textbf{`header'}} \\"
		)
	cells(
		"mean(fmt(%9.2fc) pattern(1 1 1))" 
		"sd(par fmt(%9.2fc) pattern(1 1 1) drop(*_yn_*))")  
	;
#d cr

// Improved seeds
local header "Improved seeds"
local yvarlist ma_seed_use_yn_r ma_seed_expend_usd_r
lab var ma_seed_use_yn_r 		"\quad =1 if bought any"
lab var ma_seed_expend_usd_r 	"\quad Amount spent (USD)"

est clear
eststo entire: 		estpost su `yvarlist'
eststo treatment:   estpost su `yvarlist' if GD_treat == 1  
eststo control:   	estpost su `yvarlist' if GD_treat == 0

#d ;
estout entire control treatment using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none) label 
	posthead(
		"\\"
		"\multicolumn{3}{l}{\textbf{`header'}} \\"
		)
	cells(
		"mean(fmt(%9.2fc) pattern(1 1 1))" 
		"sd(par fmt(%9.2fc) pattern(1 1 1) drop(*_yn_*))")  
	;
#d cr

// Pesticides
local header "Pesticides"
local yvarlist ma_pest_use_yn_r ma_pest_expend_usd_r
lab var ma_pest_use_yn_r 		"\quad =1 if bought any"
lab var ma_pest_expend_usd_r 	"\quad Amount spent (USD)"

est clear
eststo entire: 		estpost su `yvarlist'
eststo treatment:   estpost su `yvarlist' if GD_treat == 1  
eststo control:   	estpost su `yvarlist' if GD_treat == 0

#d ;
estout entire control treatment using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none) label 
	posthead(
		"\\"
		"\multicolumn{3}{l}{\textbf{`header'}} \\"
		)
	cells(
		"mean(fmt(%9.2fc) pattern(1 1 1))" 
		"sd(par fmt(%9.2fc) pattern(1 1 1) drop(*_yn_*))")  
	;
#d cr

// Last line (with observations)
count 
global obs_total `r(N)'
count if GD_treat==1
global obs_treat `r(N)'
count if GD_treat==0
global obs_control `r(N)'

#d;
estout * using "${raw_folder}/`texname'.tex", append style(tex) mlabel(none) collabel(none) 
	prefoot(
		"\midrule"
		"Observations & `:display %5.0fc ${obs_total}'  & `:display %5.0fc ${obs_treat}'  & `:display %5.0fc ${obs_control}' \\ "
		)
	postfoot("\bottomrule" "\end{tabular}")
	;
#d cr	
	
restore
*******************************************************************************/


/*******************************************************************************
 Table A4 : Heterogeneity by Travel Voucher Amount
*******************************************************************************/
local texname tableA4
local yvarlist fert_expend_usd fert_bought_kg

use "replicationData.dta", clear

gen GD_v200 = GD_treat*voucher_200_perKM
gen GD_v400 = GD_treat*voucher_400_perKM
gen GD_v600 = GD_treat*voucher_600_perKM

lab var voucher_200_perKM 	"MWK200 per km (Z\beta_1Z)"
lab var voucher_400_perKM 	"MWK400 per km (Z\beta_2Z)"
lab var voucher_600_perKM 	"MWK600 per km (Z\beta_2Z)"
lab var GD_treat			"Cash (Z\gammaZ)"
lab var GD_v200				"Cash Z\timesZ MWK200 (Z\delta_1Z)"
lab var GD_v400				"Cash Z\timesZ MWK400 (Z\delta_2Z)"
lab var GD_v600				"Cash Z\timesZ MWK600 (Z\delta_3Z)"
 
est clear
local i = 0
foreach yvar in `yvarlist' {
	local ++i
	
	eststo : reg ma_`yvar'_r voucher_200_perKM voucher_400_perKM voucher_600_perKM GD_treat GD_v200 GD_v400 GD_v600 i.strata if ma_sample==1, cluster(village)
		
		su ma_`yvar'_r if e(sample) & GD_treat==0 & voucher_100_flat
		estadd scalar flat_mean = r(mean)

		test voucher_200_perKM = GD_v200
		estadd scalar pval1 = r(p)

		test voucher_400_perKM = GD_v400
		estadd scalar pval2 = r(p)

		test voucher_600_perKM = GD_v600
		estadd scalar pval3 = r(p)		
}

#d;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute( \$  \\$  %  \%  Z $ xx $\times$) 
	order(voucher_200_perKM voucher_400_perKM voucher_600_perKM GD_treat GD_v200 GD_v400 GD_v600)
	keep(voucher_200_perKM voucher_400_perKM voucher_600_perKM GD_treat GD_v200 GD_v400 GD_v600)
	stats(pval1 pval2 pval3 flat_mean N, labels("\emph{p}-values: \\ \quad Z\beta_1=\delta_1Z" "\quad Z\beta_2=\delta_2Z" "\quad Z\beta_3=\delta_3Z" "Flat MWK100: Mean" "Observations") fmt(%9.3f %9.3f %9.3f %9.2f %9.0fc))
	starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l cc cc}" "\toprule")
	posthead( 
	" & \multicolumn{1}{c}{\begin{tabular}{c} Total \\ fertilizer \\ expenditure \\ (USD) \end{tabular}} & \multicolumn{1}{c}{\begin{tabular}{c} Total \\ purchased \\ amount \\ (KG) \end{tabular}} \\"
	"\midrule" )
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")
	;
#d cr
*******************************************************************************/


/*************************************************************
 Table A5: FISP Beneficiary Status and Treatment Assignment
*************************************************************/
local texname tableA5
local yvarlist fisp_hh_2019

use "replicationData.dta", clear

est clear
local i = 0
foreach yvar in `yvarlist'{
	local ++i
		
	eststo : reg `yvar' GD_only MA_only GD_MA_both i.strata fisp_hh_2018 if reg_sample==1, cluster(village) 
	
		su `yvar' if e(sample) & GD_treat==0 & MA_treat==0
		estadd scalar pure_control_mean = r(mean) 
					
		test GD_only = GD_MA_both 
		estadd scalar pval1 = r(p)
	
}
		
#d ;
estout * using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 
	keep(GD_only MA_only GD_MA_both)
	order(GD_only MA_only GD_MA_both)
	stats(pval1 pure_control_mean N, labels("\emph{p}-value: \\ \quad Z\beta = \delta Z"  "Pure control mean" "Observations") fmt(%9.3f %9.2f %9.0fc)) starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l c}" "\toprule")
	posthead(
		"& \multicolumn{1}{c}{\begin{tabular}{c} =1 if received FISP voucher \\ after intervention (2019-2020) \end{tabular}}  \\" 
		"\midrule" 
		)
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")	
	;
	
#d cr
*************************************************************/	


/**************************************************************
 Table A6: Crop Choice and Agricultural Productivity
**************************************************************/
local texname tableA6
local yvarlist ///
	staple_yn yield_kg_staple harvest_usd_staple ///
	cereal_nonstaple_num legume_num crops_num ///
	harvest_usd_nonstaple 
local numcol = wordcount("`yvarlist'")
local column_header ///
	" & \multicolumn{3}{c}{Maize (staple)} & \multicolumn{3}{c}{Number of crops planted/harvested:} & \multirow{5}{*}{\begin{tabular}{c} Total value \\ of non-staples \\ harvested \\ (USD) \end{tabular}} \\" ///
	"\cmidrule(lr){2-4}\cmidrule(lr){5-7}" ///
	" & \begin{tabular}{c} =1 if \\ planted \\ any \end{tabular} & \begin{tabular}{c} Amount \\ harvested \\ (kg) \end{tabular}  & \begin{tabular}{c} Value of \\ harvest \\ (USD)\end{tabular} & \begin{tabular}{c} Non-staple \\ cereals\tnote{a} \end{tabular} & Legumes\tnote{b} & \begin{tabular}{c} All crops \\ pooled \end{tabular} \\"
		
use "replicationData.dta", clear
	
est clear
local i = 0
foreach yvar in `yvarlist'{
	local ++i
	
	eststo overall_v3_`i' : reg `yvar' GD_only MA_only GD_MA_both `yvar'_y0_m `yvar'_y0_r ${controls} i.strata if reg_sample==1, cluster(village) 		

		su `yvar' if e(sample) & GD_treat==0 & MA_treat==0 
		estadd scalar pure_control_mean = r(mean) 
					
		test GD_only = GD_MA_both 
		estadd scalar pval1 = r(p)				
}

#d ;
estout overall_* using "${raw_folder}/`texname'.tex", replace style(tex) mlabel(none) collabel(none) number label 
	substitute(  "\$"  \\$  %  \% xx $\times$ Z $ ) 
	keep(GD_only MA_only GD_MA_both)
	order(GD_only MA_only GD_MA_both)
	stats(pval1 pure_control_mean N, labels("\emph{p}-value: \\ \quad Z\beta = \delta Z"  "Pure control mean" "Observations") fmt(%9.3f %9.2f %9.0fc)) starlevels(* 0.10 ** 0.05 *** 0.01) /* stardrop(*) */
	prehead("\begin{tabular}{l *{`numcol'}{c}}" "\toprule")
	posthead(
		"`column_header'" 
		"\midrule" 
		)
	cells("b(fmt(%9.2f) star)" "se(fmt(%9.2f) par)") 
	prefoot("\\")
	postfoot("\bottomrule" "\end{tabular}")	
	;
	
#d cr
**************************************************************/




exit 

